﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Core;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class AnnotationWrapperView : View, IAnnotationWrapperView
    {
        public AnnotationWrapperView()
        {
            InitializeComponent();

            this.cmdComment.Click += (o, e) => { this.CommentButtonClicked.Raise(this); };
            this.chkIsImportant.CheckedChanged += (o, e) => { this.ImportanceChanged.Raise(this); };
            this.Load += new EventHandler(AnnotationWrapperView_Load);
        }

        private void AnnotationWrapperView_Load(object sender, EventArgs e)
        {
            //attach handler to the parent form paint event - once the parent has been painted once any listeners can be notified this view has loaded
            Form parent = this.FindForm();
            parent.Paint += new PaintEventHandler(Parent_Paint);
            this.Load -= this.AnnotationWrapperView_Load;
        }

        private void Parent_Paint(object sender, PaintEventArgs e)
        {
            ((Form)sender).Paint -= this.Parent_Paint;
            this.Shown.Raise(this);
        }

        [Region("Wrapped")]
        public Panel MainPanel
        {
            get { return pnlWrappedAnnotation; }
        }

        public event EventHandler CommentButtonClicked;
        public event EventHandler ImportanceChanged;
        public event EventHandler<ViewedAnnotationIdChangedEventArgs> ViewedAnnotationIdChanged;

        private void cmdComment_Click(object sender, EventArgs e)
        {
            CommentButtonClicked(this, EventArgs.Empty);
        }
    
        public event EventHandler Shown;

        public Image GetViewImage()
        {
            Application.DoEvents();
            Form f = this.FindForm();
            return ScreenCapture.CaptureWindow(f.Handle);
        }

        public bool IsImportant
        {
            get { return this.chkIsImportant.Checked; }
        }

        public string WrappedViewTitle { get; set; }

        public override string Title
        {
            get { return this.WrappedViewTitle ?? base.Title; }
        }
    }
}
